package com.zygh.lz.controller.app;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.zygh.lz.controller.FileUpload;
import com.zygh.lz.dao.StaffMapper;
import com.zygh.lz.entity.Problem;
import com.zygh.lz.entity.Staff;
import com.zygh.lz.entity.vo.ProblemVO;
import com.zygh.lz.service.ProblemService;
import com.zygh.lz.stapprove.*;
import com.zygh.lz.util.ListToStringTest;
import com.zygh.lz.vo.ResultBean;
import com.zygh.lz.vo.ResultBeans;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@Api(tags = "App问题模块", description = "AppProblemController")
public class AppProblemController {
    @Resource
    private ProblemService problemService;
    @Resource
    private StaffMapper staffMapper;


    @ApiOperation("APP问题记录列表")
    @PostMapping("selectProblemPecord")
    @ViLog(logType = "1", module = "问题模块>问题模糊查询分层")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", dataType = "Integer", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", dataType = "Integer", required = true, paramType = "query")

    })
    public OutBean selectProblemPecord(@RequestBody(required = false) InBean inBean) {

        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        Integer staffId = Integer.parseInt(jsonObject.getString("staffId"));
        Integer current = Integer.valueOf(jsonObject.getString("pageNo"));
        Integer size = Integer.valueOf(jsonObject.getString("pageSize"));

        ResultBeans resultBeans = problemService.selectProblemPecord(staffId, current, size);
        return OutBeanUtil.resSuc(resultBeans, inBean);
    }

    @ApiOperation("APP问题核查列表")
    @PostMapping("selectProblemCheck")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", dataType = "Integer", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "每页数量", dataType = "Integer", required = true, paramType = "query")
    })
    @ViLog(logType = "1", module = "问题模块>按条件查询没有核查的问题")
    public OutBean selectProblemCheck(@RequestBody(required = false) InBean inBean) {
        String url = "http://62.66.6.163:8787/Vehicle/findVehicleByHphm";
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        String hphm = jsonObject.getString("hphm");
        String hpzl = jsonObject.getString("type");
        //ResultBeans resultBeans = problemService.selectProblemCheck(staffId, problemStrat, current, size);
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("hphm", hphm.substring(1));
        paramMap.put("hpzl", hpzl);
        String result = HttpUtil.get(url, paramMap);
        if (StringUtils.isEmpty(result)) {
            return OutBeanUtil.resSuc(ResultUtil.resultBeansSuccess(null), inBean);
        }
        return OutBeanUtil.resSuc(ResultUtil.resultBeansSuccess(result), inBean);
    }



    @ApiOperation("APP问题新增")
    @PostMapping("addProblem")
    @ViLog(logType = "2", module = "问题模块>问题新增")
    public OutBean addProblem(Problem problem, @RequestBody(required = false) InBean inBean) {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        problem.setProblemType(Integer.parseInt(jsonObject.getString("problemType")));
        problem.setProblemDescribe(jsonObject.getString("problemDescribe"));
        problem.setProblemState(jsonObject.getString("problemState"));
        problem.setProblemGpsX(jsonObject.getString("problemGpsX"));
        problem.setProblemGpsY(jsonObject.getString("problemGpsY"));
        problem.setProblemDate(new Date());
        problem.setProblemCheck(jsonObject.getString("problemCheck"));
        problem.setProblemDetail(jsonObject.getString("problemDetail"));
        //problem.setSysPatrolRecordId(Integer.parseInt(jsonObject.getString("sysPatrolRecordId")));
        int sysStaffId = Integer.parseInt(jsonObject.getString("sysStaffId"));
        problem.setSysStaffId(sysStaffId);
        //problem.setRoadName(jsonObject.getString("roadName"));

        //获取上报人本大队指挥室账号id
        Staff staff = staffMapper.selectInfoByid(sysStaffId);
        String sectionName = staff.getSectionName();
        String battalion = sectionName.substring(0, sectionName.indexOf("队") + 1);
        Staff staffByName = staffMapper.findStaffByName(battalion + "指挥中心");
        if (staffByName != null) {
            problem.setAcceptId(staffByName.getSysStaffId());
        } else {
            problem.setAcceptId(1);
        }

        String problemPicture = jsonObject.getString("picture");
        //第一种存成Base64
        net.sf.json.JSONArray dataJson = net.sf.json.JSONArray.fromObject(problemPicture);
        List<String> list = (List<String>) JSONArray.toCollection(dataJson, String.class);
        FileUpload fileUpload = new FileUpload();
        List<String> list1 = fileUpload.uploadPictures(list);
        String image = ListToStringTest.listToString1(list1, ',');
        problem.setProblemPicture(image);
        //problem.setProblemPicture(problemPicture);
        problem.setProblemVideo(jsonObject.getString("problemVideo"));
        ResultBeans resultBeans = problemService.addProblem(problem);
        return OutBeanUtil.resSuc(resultBeans, inBean);
    }


    @ApiOperation("修改问题")
    @PostMapping("updateProblem")
    @ViLog(logType = "3", module = "问题模块>问题修改")
    public ResultBean updateProblem(@RequestBody ProblemVO problem) throws InvocationTargetException, IllegalAccessException {
        return problemService.updateProblem(problem);
    }

    @ApiOperation("根据主键id查询问题详情")
    @PostMapping("selectProblemByid")
    @ViLog(logType = "1", module = "问题模块>根据主键id查询问题详情")
    public OutBean selectProblemByid(@RequestBody(required = false) InBean inBean) throws ExecutionException, InterruptedException {
        JSONObject jsonObject = inBean.convertObj(JSONObject.class);
        ResultBeans resultBeans = problemService.selectProblemByid(Integer.parseInt(jsonObject.getString("id")), Integer.parseInt(jsonObject.getString("type")));
        return OutBeanUtil.resSuc(resultBeans, inBean);
    }
}
